package com.mengfx.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mengfx.bean.Product;
import com.mengfx.result.ProductResult;
import com.mengfx.service.ProductService;
import com.mengfx.utils.ResponseObject;
import com.mengfx.vo.ProductVo;


@Controller
@RequestMapping("/product")
public class ProductController {
	
	@Autowired
	private ProductService productService;
	
	@ResponseBody
	@RequestMapping("/productlist")
	public ResponseObject<ProductResult> productList(@RequestParam(value = "page",required=false) Integer page,String pt){
		int p;
		if(null == page){
			p = 0;
		}else
			p = Integer.valueOf(page);
		List<Product> list = productService.getProductByPage(p,10,pt);
		
		int count = productService.getProductCount();
		ProductResult result = new ProductResult();
		
		List<ProductVo> volist = new ArrayList<ProductVo>();
		result.setCount(count);
		
		for(Product product : list){
			ProductVo vo = new ProductVo();
			vo.setProductId(product.getProductId());
			vo.setProductName(product.getManufacture());
			volist.add(vo);
		}
		
		result.setProductVo(volist);
		result.setCurrentpage(p);
		return new ResponseObject<ProductResult>(result);
		
	}
	
	@ResponseBody
	@RequestMapping("/search/name")
	public ResponseObject<List<Product>> searchByName(String name){
		return new ResponseObject<List<Product>>(productService.searchProductByName(name));
	}
	
}
